/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.yuecai.modules.yc_user_coupon;


import com.yuecai.modules.yc_user_coupon.service.YcUserCouponService;
import com.yuecai.modules.yc_user_coupon.service.dto.YcUserCouponQueryCriteria;
import com.yuecai.utils.SecurityUtils;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;

/**
* @website https://el-admin.vip
* @author dm
* @date 2020-07-21
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "yc_user_coupon管理")
@RequestMapping("/ycUserCoupon")
public class YcUserCouponController {

    private final YcUserCouponService ycUserCouponService;



    @GetMapping(value = "/download")
    public void download(HttpServletResponse response, YcUserCouponQueryCriteria criteria) throws IOException {
        String currentUsername = SecurityUtils.getCurrentUsername();
        Long aLong = Long.valueOf(currentUsername);
        criteria.setUserId(aLong);
        criteria.setExpireDate(new Timestamp(System.currentTimeMillis()));
        ycUserCouponService.download(ycUserCouponService.queryAll(criteria), response);
    }

    @GetMapping
    public ResponseEntity<Object> query(YcUserCouponQueryCriteria criteria, Pageable pageable){
        String currentUsername = SecurityUtils.getCurrentUsername();
        Long aLong = Long.valueOf(currentUsername);
        criteria.setUserId(aLong);
        if (criteria.getIsUse()==0){
            criteria.setExpireDate(new Timestamp(System.currentTimeMillis()));
        }
        return new ResponseEntity<>(ycUserCouponService.queryAll(criteria,pageable),HttpStatus.OK);
    }



    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        ycUserCouponService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}